package com.winstar.controller;

import com.winstar.entity.NumberAttribution;
import com.winstar.repository.NumberAttributionRepository;
import com.winstar.utils.StrUtil;
import com.winstar.vo.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


/**
 *@author  shoo on 2017/1/4.
 * @describe  号码归属地查询
 */
@Api(value = "attribution api", description = "号码归属地查询", position = 5)
@RestController
@RequestMapping("/api/v1/attribution")
public class NumberAttributionController {

    @Autowired
    private NumberAttributionRepository numberAttributionRepository;

    @ApiOperation(value = "查询手机号码归属地", httpMethod = "GET", response = ResponseEntity.class, notes = "请输入正确的11位手机号查询" , position = 2)
    @RequestMapping(value = "/{phone}/phone" ,method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResponseEntity selAttribution(@ApiParam(value = "手机号") @PathVariable String phone){

        Message msg = new Message();
     /*   if(!StrUtil.validatePhone(phone)){
            msg.setText("手机号有误");
            msg.setStatus("失败");
            return new ResponseEntity<>(msg, HttpStatus.BAD_REQUEST);
        }*/
        if(phone.length()!=11){
            msg.setText("手机号有误");
            msg.setStatus("失败");
            return new ResponseEntity<>(msg, HttpStatus.BAD_REQUEST);
        }
        phone = phone.substring(0,7);
        NumberAttribution numberAttribution = numberAttributionRepository.findByPhone(phone);
        if(ObjectUtils.isEmpty(numberAttribution)){
            msg.setText("库中不存在");
            msg.setStatus("失败");
            return new ResponseEntity<>(msg, HttpStatus.BAD_REQUEST);
        }
        msg.setText(numberAttribution.getProvince()+numberAttribution.getCity());
        msg.setStatus("成功");
        return new ResponseEntity<>(msg, HttpStatus.OK);
    }

    @ApiOperation(value = "查询手机号码归属地、邮政编码、行政区域编码、区号", httpMethod = "GET", response = ResponseEntity.class, notes = "请输入正确的11位手机号查询" , position = 1)
    @RequestMapping(value = "/{phoneNumber}/phoneNumber" ,method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResponseEntity selAll(@ApiParam(value = "手机号") @PathVariable String phoneNumber){

        Message msg = new Message();
        /*if(!StrUtil.validatePhone(phoneNumber)){
            msg.setText("手机号有误");
            msg.setStatus("失败");
            return new ResponseEntity<>(msg, HttpStatus.BAD_REQUEST);
        }*/
        if(phoneNumber.length()!=11){
            msg.setText("手机号有误");
            msg.setStatus("失败");
            return new ResponseEntity<>(msg, HttpStatus.BAD_REQUEST);
        }
        phoneNumber = phoneNumber.substring(0,7);
        NumberAttribution numberAttribution = numberAttributionRepository.findByPhone(phoneNumber);
        if(ObjectUtils.isEmpty(numberAttribution)){
            msg.setText("库中不存在");
            msg.setStatus("失败");
            return new ResponseEntity<>(msg, HttpStatus.BAD_REQUEST);
        }
        JSONObject jsonObject = JSONObject.fromObject(numberAttribution);
        msg.setText(jsonObject.toString());
        msg.setStatus("成功");
        return new ResponseEntity<>(msg, HttpStatus.OK);
    }
}
